import time
from selenium import webdriver

start_time = time.time()

# 懒加载
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://search.suning.com/%E7%A9%BA%E8%B0%83/&isNoResult=0&ci=431505&iy=-1")

counter = 0
page = 0
while True:
    page += 1
    time.sleep(3)
    # 下滑动作
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    time.sleep(3)
    div_list = driver.find_elements("xpath", "//div[@class='item-bg']")
    for div in div_list:
        counter += 1
        price = div.find_element("xpath", ".//span[@class='def-price']").text
        title = div.find_element("xpath", ".//div[@class='title-selling-point']/a").text
        url = div.find_element("xpath", ".//div[@class='title-selling-point']/a").get_attribute("href")
        try:
            comment_count = div.find_element("xpath", ".//div[@class='info-evaluate']/a/i").text
        except:
            comment_count = "暂无评价"
        print(page, counter, price, title, url, comment_count)
    try:
        driver.find_element("xpath", "//a[@class='page-right']").click()
    except Exception as e:
        print(type(e), e)
        input()
        break

print("总耗时：", time.time() - start_time)
